rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(ggplot2)
library(CausalImpact)

## Estimate the causal effect of the pandemic on the trend of Asian hate crimes via Bayesian structural time-series models

crimes <- fread('./data/hate_crimes_classified.csv')
crimes[,date:=as.Date(crimes$Date, '%d-%m-%Y')]
crimes[,month:=as.Date(cut(date,breaks = "month", start.on.monday = FALSE))]

trend_asian <- crimes[region_broad=='Asia', .(asian = .N), by=month]
trend_african <- crimes[region_broad=='Africa', .(african = .N), by=month]
trend_samerican <- crimes[region_broad=='Central and South America', .(samerican=.N),by=month]
trend_seuropean <- crimes[region_broad=='Eastern and Southern Europe', .(seuropean=.N),by=month]
trend_smideastern <- crimes[region_broad=='Middle East', .(mideastern=.N),by=month]
trend_namerican <- crimes[region_broad=='North America', .(namerican=.N),by=month]
trend_neuropean <- crimes[region_broad=='Northern and Western Europe', .(neuropean=.N),by=month]

months <- crimes[,.(month=unique(month))]

data <- merge(months, trend_asian, by='month', all.x=TRUE)
data <- merge(data, trend_african, by='month', all.x = TRUE)
data <- merge(data, trend_samerican, by='month', all.x = TRUE)
data <- merge(data, trend_seuropean, by='month', all.x = TRUE)
data <- merge(data, trend_smideastern, by='month', all.x = TRUE)
data <- merge(data, trend_namerican, by='month', all.x = TRUE)
data <- merge(data, trend_neuropean, by='month', all.x = TRUE)

ind <-   which(sapply(data, is.numeric))
for(j in ind){
  set(data, i = which(is.na(data[[j]])), j = j, value = 0)
}

data <- zoo(cbind(data$asian, data$african, data$samerican, data$seuropean, data$mideastern, data$namerican, data$neuropean), data$month)
colnames(data) <- c('y', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6')

pre.period <- as.Date(c("2007-01-01", "2020-01-01"))
post.period <- as.Date(c("2020-02-01", "2020-03-01"))


impact <- CausalImpact(data, pre.period, post.period, model.args = list(niter=2000, nseasons=12))

impact.plot <- plot(impact, c("original", "pointwise")) +
  annotate("text", x = as.Date('03-01-2019', format='%m-%d-%Y'), y = 40, label = "Covid-19", colour='blue') +
  scale_x_date(breaks = "6 month", minor_breaks = "1 month", date_labels = '%Y-%m',
               limits = c(min(months$month), max = as.Date('04-01-2020', format='%m-%d-%Y')),
               expand=c(0,0)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position="none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)),axis.text.y=element_text(size=12))

impact.plot
ggsave('./causal_effect_time_series_asian.pdf', width=6, height=4.85)

